package com.loginlib.table;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loginlib.R;
import com.loginlib.di.component.DaggerTableActivityComponent;
import com.loginlib.di.component.TableActivityComponent;
import com.loginlib.di.module.TableModule;
import com.loginlib.presenters.TableBillPresenter;
import com.loginlib.table.adapter.TableBillAdapter2;
import com.orhanobut.logger.Logger;
import com.wanhe.base.BaseActivity;
import com.wanhe.base.model.disgn.OrderedBill;
import com.wanhe.base.model.holder.TableInfoHolder;
import com.wanhe.base.util.SPUtils;
import com.wanhe.base.util.ViewUtil;
import com.wanhe.base.websocket.WebSocketTool;
import com.wanhe.base.websocket.callback.CartStateCallBack;
import com.wanhe.base.websocket.callback.OpenTableCallBack;
import com.wanhe.base.websocket.model.BillDish;
import com.wanhe.base.websocket.model.response.BaseResponse;
import com.wanhe.shoppingcart.bean.CartStateDish;
import com.wanhe.shoppingcart.bean.CartStateResponse;
import com.wanhe.shoppingcart.counter.CounterManager;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by Administrator on 2016-08-30.
 */
public class TableBillActivity extends BaseActivity implements View.OnClickListener ,TableBillPresenter.OnGetTableBillListener ,
        OpenTableCallBack {

    public static final String TABLEID = "table_id";
    public static final String TABLEID_UUID= "table_uudi";
    public static final String TABLESTATE = "tablestate";
    public static final String TABLE_BILLAMOUNT = "BillAmount";
    private TextView tvTitle;
    private TextView tvConfrim;
    private TextView tvCancle;
    private LinearLayout llButton;
    private TextView tvPayBillBtn;
    private TextView tv_closeCheckout;
    private ExpandableListView lv_ordered_bill;
    private String tableId;
    private String tableUUid;
    public TableActivityComponent tableActivityComponent;
    @Inject
    TableBillPresenter tableBillPresenter;
    private double billAmount;
    private TableBillAdapter2 mTableBillAdapter;


    @Override
    protected void initComponent(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_tablebill_layout);
        initViewId();
        initInject();
        initData();

    }


    public void initInject(){
        tableActivityComponent = DaggerTableActivityComponent.builder().tableModule(new TableModule()).build();
        tableActivityComponent.inject(this);
    }
//


    private void initViewId() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvConfrim = (TextView) findViewById(R.id.tv_Confrim);
        tvCancle = (TextView) findViewById(R.id.tv_Cancle);
        llButton = (LinearLayout) findViewById(R.id.ll_button);
        tvPayBillBtn = (TextView) findViewById(R.id.tv_pay_bill_btn);
        tv_closeCheckout = (TextView) findViewById(R.id.tv_closeCheckout);
        lv_ordered_bill = (ExpandableListView) findViewById(R.id.lv_ordered_bill);
        tvConfrim.setOnClickListener(this);
        tvCancle.setOnClickListener(this);
        tvPayBillBtn.setOnClickListener(this);
        tv_closeCheckout.setOnClickListener(this);
    }
//

    protected void initData() {
        tableId = getIntent().getStringExtra(TABLEID);
        tableUUid  = getIntent().getStringExtra(TABLEID_UUID);
        billAmount = getIntent().getDoubleExtra(TABLE_BILLAMOUNT, 0);
        tvTitle.setText(tableId + "桌  " + "  合计：" + billAmount + "元");
        mTableBillAdapter = new TableBillAdapter2(this);
        lv_ordered_bill.setAdapter(mTableBillAdapter);
        lv_ordered_bill.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
        tableBillPresenter.setOnGetTableBillListener(this);
//        tableBillPresenter.getTableBill(tableId, SPUtils.getAccount(),SPUtils.getPassword());
//        instance = WaiterManager.getInstance();
        tableBillPresenter.getTableBill2(tableUUid);
//        // TODO: 2016-08-30 需要判断
//        if (instance.isLogin()) {
//            Waiter waiter = WaiterManager.getInstance().getWaiter();
//            account = waiter.getAccount();
//            pass = waiter.getPassword();
//            tableBillPresenter.setPayBillWeakReference(this);
//            tableBillPresenter.getDiscountType();
//        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_Confrim) {
            tableBillPresenter.openTable(tableUUid,  TableInfoHolder.getInstance().getCustomNum(),this);
        } else if (i == R.id.tv_Cancle) {//点返回清空人数
            TableInfoHolder.getInstance().setCustomNum(0);
            finish();
        } else if (i == R.id.tv_pay_bill_btn) {
        } else if (i == R.id.tv_closeCheckout) {
        } else {
        }
    }
    @Override
    public void onGetTableBillSuccess(ArrayList<OrderedBill> arrOrderedBill) {
        if (mTableBillAdapter != null) {
            mTableBillAdapter.setData(arrOrderedBill);
            for (int i = 0; i < mTableBillAdapter.getGroupCount(); i++) {
                if(lv_ordered_bill != null){
                    lv_ordered_bill.expandGroup(i);
                }

            }
        }
    }

    @Override
    public void onGetTableBillFail(String msg) {
        ViewUtil.ShortToast(this, msg);
    }

    @Override
    public void onOpenTable(String body, short source) {
        Gson gson = new Gson();
        BaseResponse response =   gson.fromJson(body, BaseResponse.class);
        if(response.getCode()==0){
            SPUtils.setKeySource(String.valueOf(source));
//            WebSocketTool.getInstance().bid = bid;
            Intent intent = new Intent();
            intent.setAction("com.wanhe.meal");
            startActivity(intent);
        }else{
            ViewUtil.ShortToast(this,response.getMessage());
        }
    }

}
